/*   1:    */ package com.jetbrains.licenseServer.openapi.util;
/*   2:    */ 
/*   3:    */

import com.jetbrains.licenseServer.openapi.PublicKeys;
import com.jetbrains.licenseServer.openapi.requests.AbstractRequest;
import com.jetbrains.licenseServer.openapi.responses.AbstractResponse;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;
import java.security.SignatureException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */ 
/*  24:    */ public class RpcUtil
/*  25:    */ {
/*  26:    */   private static final int a = 15000;
/*  27: 26 */   private static Set<String> b = new HashSet(Arrays.asList(new String[] { "actionName", "class" }));
/*  28:    */   
/*  29:    */   private static void a(String paramString1, String paramString2)
/*  30:    */     throws VerificationFailedException
/*  31:    */   {
/*  32:    */     boolean bool;
/*  33:    */     try
/*  34:    */     {
/*  35: 33 */       if ((!paramString2.startsWith("<!-- ")) || (!paramString2.endsWith(" -->"))) {
/*  36: 34 */         throw new VerificationFailedException("Validation failed");
/*  37:    */       }
/*  38: 37 */       byte[] arrayOfByte = new byte[(paramString2.length() - "<!-- ".length() - " -->".length()) / 2];
/*  39:    */       
/*  40: 39 */       int i = "<!-- ".length();
/*  41: 40 */       for (int j = 0; j < arrayOfByte.length; j++)
/*  42:    */       {
/*  43: 41 */         int k = a(paramString2.charAt(i++));
/*  44: 42 */         int m = a(paramString2.charAt(i++));
/*  45: 43 */         arrayOfByte[j] = ((byte)((k << 4) + m));
/*  46:    */       }
/*  47: 46 */       Signature localSignature = PublicKeys.createVerifyingSignature();
/*  48: 47 */       localSignature.update(paramString1.getBytes());
/*  49:    */       
/*  50: 49 */       bool = localSignature.verify(arrayOfByte);
/*  51:    */     }
/*  52:    */     catch (NoSuchAlgorithmException localNoSuchAlgorithmException)
/*  53:    */     {
/*  54: 52 */       throw new VerificationFailedException(localNoSuchAlgorithmException);
/*  55:    */     }
/*  56:    */     catch (InvalidKeyException localInvalidKeyException)
/*  57:    */     {
/*  58: 55 */       throw new VerificationFailedException(localInvalidKeyException);
/*  59:    */     }
/*  60:    */     catch (SignatureException localSignatureException)
/*  61:    */     {
/*  62: 58 */       throw new VerificationFailedException(localSignatureException);
/*  63:    */     }
/*  64: 60 */     if (!bool) {
/*  65: 61 */       throw new VerificationFailedException("Verification failed");
/*  66:    */     }
/*  67:    */   }
/*  68:    */   
/*  69:    */   public static String executeRpc(URL paramURL)
/*  70:    */     throws IOException, VerificationFailedException
/*  71:    */   {
/*  72: 66 */     URLConnection localURLConnection = paramURL.openConnection();
/*  73:    */     
/*  74: 68 */     localURLConnection.setReadTimeout(15000);
/*  75: 69 */     localURLConnection.setConnectTimeout(15000);
/*  76:    */     
/*  77: 71 */     BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localURLConnection.getInputStream()));
/*  78: 72 */     String str1 = localBufferedReader.readLine();
/*  79: 73 */     String str2 = localBufferedReader.readLine();
/*  80:    */     
/*  81: 75 */     localBufferedReader.close();
/*  82:    */     
/*  83: 77 */     a(str2, str1);
/*  84:    */     
/*  85: 79 */     return str2;
/*  86:    */   }
/*  87:    */   
/*  88:    */   public static <T extends AbstractResponse> T executeRpc(String paramString, AbstractRequest<T> paramAbstractRequest)
/*  89:    */     throws IOException, VerificationFailedException
/*  90:    */   {
/*  91: 84 */     StringBuilder localStringBuilder = new StringBuilder();
/*  92: 85 */     localStringBuilder.append(paramString).append("/rpc/").append(paramAbstractRequest.getActionName());
/*  93:    */     try
/*  94:    */     {
/*  95: 87 */       PropertyDescriptor[] arrayOfPropertyDescriptor = Introspector.getBeanInfo(paramAbstractRequest.getClass()).getPropertyDescriptors();
/*  96: 88 */       int i = 1;
/*  97: 89 */       for (Object localObject2 : arrayOfPropertyDescriptor)
/*  98:    */       {
/*  99: 90 */         String str2 = localObject2.getName();
/* 100: 91 */         if (!b.contains(str2))
/* 101:    */         {
/* 102: 94 */           Object localObject3 = localObject2.getReadMethod().invoke(paramAbstractRequest, new Object[0]);
/* 103: 95 */           if (localObject3 != null)
/* 104:    */           {
/* 105: 98 */             if (i != 0)
/* 106:    */             {
/* 107: 99 */               localStringBuilder.append("?");
/* 108:100 */               i = 0;
/* 109:    */             }
/* 110:    */             else
/* 111:    */             {
/* 112:103 */               localStringBuilder.append("&");
/* 113:    */             }
/* 114:105 */             localStringBuilder.append(str2).append("=").append(URLEncoder.encode(localObject3.toString(), "utf-8"));
/* 115:    */           }
/* 116:    */         }
/* 117:    */       }
/* 118:    */     }
/* 119:    */     catch (Exception localException)
/* 120:    */     {
/* 121:109 */       throw new VerificationFailedException("Error creating request", localException);
/* 122:    */     }
/* 123:112 */     URL localURL = new URL(localStringBuilder.toString());
/* 124:113 */     String str1 = executeRpc(localURL);
/* 125:114 */     ??? = ResponseMarshaller.unmarshall(str1);
/* 126:116 */     if (((AbstractResponse)???).getSalt() != paramAbstractRequest.getSalt()) {
/* 127:117 */       throw new VerificationFailedException("Salt from server does not equals to salt in request");
/* 128:    */     }
/* 129:120 */     return ???;
/* 130:    */   }
/* 131:    */   
/* 132:    */   private static int a(char paramChar)
/* 133:    */     throws VerificationFailedException
/* 134:    */   {
/* 135:124 */     if (('0' <= paramChar) && (paramChar <= '9')) {
/* 136:125 */       return paramChar - '0';
/* 137:    */     }
/* 138:127 */     if (('A' <= paramChar) && (paramChar <= 'F')) {
/* 139:128 */       return paramChar - 'A' + 10;
/* 140:    */     }
/* 141:130 */     if (('a' <= paramChar) && (paramChar <= 'f')) {
/* 142:131 */       return paramChar - 'a' + 10;
/* 143:    */     }
/* 144:133 */     throw new VerificationFailedException("Unexpected character in signature");
/* 145:    */   }
/* 146:    */ }



/* Location:           E:\idea\

 * Qualified Name:     com.jetbrains.licenseServer.openapi.util.RpcUtil

 * JD-Core Version:    0.7.0.1

 */